//
//  RegistViewController.m
//  FlowerRainGround
//
//  Created by lanou on 16/4/19.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "RegistViewController.h"
#import "SpecialViewController.h"
#import "RootTabBatController.h"
#import "LoginViewController.h"
@interface RegistViewController ()<UIImagePickerControllerDelegate, UINavigationControllerDelegate>
@property (strong, nonatomic) IBOutlet UITextField *PhoneNumber;
@property (strong, nonatomic) IBOutlet UITextField *PassWord;
@property (strong, nonatomic) IBOutlet UITextField *Code;
@property (strong, nonatomic) IBOutlet UIImageView *headImageView;

@property (strong, nonatomic) IBOutlet UITextField *UserName;


@end

@implementation RegistViewController


//发送验证码
- (IBAction)GetCode:(id)sender {
    
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [manager POST:REQUESTREGISTCODE parameters:@{@"action":@"send",@"mcc":@"460",@"mobile":self.PhoneNumber.text } progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
        NSLog(@"注册：%@", dic);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
    }];
    

}






//注册信息
- (IBAction)registRequestData:(id)sender {
    NSLog(@"1111");
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager POST:REQUESTREGUST parameters:@{@"action":@"regist",@"jpushCode":@"09169d76b7d",@"market":@"appstore",@"mobile":self.PhoneNumber.text,@"password":self.PassWord.text,@"userName": self.UserName.text ,@"terminal":@"ios", @"verifyCode":self.Code.text} progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves error:nil];
        
        NSLog(@"aaaaa%@",dic);
        dispatch_async(dispatch_get_main_queue(), ^{
            NSNumber *status = dic[@"status"];
            
            //如果注册失败显示失败原因
            if ([status intValue] == 0) {
                NSLog(@"注册失败:%@",dic[@"msg"]);
                NSString *msg = dic[@"msg"];
                UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"注册失败" message:msg preferredStyle:UIAlertControllerStyleAlert];
                [alertVC addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    [alertVC dismissViewControllerAnimated:YES completion:nil];
                }]];
                [self presentViewController:alertVC animated:YES completion:nil];
            }else{
                //注册成功保存用户信息
                NSLog(@"注册成功");
                [userManager saveuserID:dic[@"result"][@"id"] ];
                [userManager saveserMobile:dic[@"result"][@"mobile"]];
                [userManager savePassWord:dic[@"result"][@"password"]];
                [userManager saveuserName:self.UserName.text];
                [userManager saveuserHeadImage:self.headImageView.image];
                //注册成功跳转到主页面
                RootTabBatController *speciaVC = [[RootTabBatController alloc] init];
                [self presentViewController:speciaVC animated:YES completion:nil];
            }
        });
        

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
    }];
}

- (void)chooseimage
{
    self.headImageView.userInteractionEnabled = YES;
    self.headImageView.layer.cornerRadius = 40;
    self.headImageView.layer.masksToBounds = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
    [self.headImageView addGestureRecognizer:tap];
}

//轻拍头像选择头像
- (void) tap:(UITapGestureRecognizer *)tapGesture{
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"选择头像" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    [alertVC addAction:[UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        //如果有相机
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            UIImagePickerController *pickerVC = [[UIImagePickerController alloc] init];
            pickerVC.delegate = self;
            pickerVC.allowsEditing = YES;
            
            [pickerVC presentViewController:pickerVC animated:YES completion:nil];
        }
        
    }]];
    [alertVC addAction:[UIAlertAction actionWithTitle:@"从相册中选取" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        picker.delegate = self;
        picker.allowsEditing = YES;
        [self presentViewController:picker animated:YES completion:nil];
    }]];
    
    [alertVC addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [alertVC dismissViewControllerAnimated:YES completion:nil];
    }]];
    
    [self presentViewController:alertVC animated:YES completion:nil];
    
}

//从相册中获取头像
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    self.headImageView.image = [info objectForKey:UIImagePickerControllerOriginalImage];
    [self dismissViewControllerAnimated:YES completion:nil];
}


- (IBAction)backLoginView:(id)sender {
    LoginViewController *login = [[LoginViewController alloc] init];
    [self presentViewController:login animated:YES completion:nil];
}







- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [self chooseimage];
    
    
    
    
    
    
    
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
